package com.dcbeida.activity.zyff;

import android.os.Bundle;

import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.activity.CaptureActivity;
import com.dcbeida.bean.CpreSendDrug;
import com.dcbeida.bean.DrugCode2Info;
import com.dcbeida.utils.Constants;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;


public class DrugGiveConfimActivity extends BaseActivity implements DrugGiveConfirmContract.View {

    @Bind(R.id.img_back)
    ImageView img_back;

    @Bind(R.id.tv_end_confim)
    TextView tv_end_confim;

    @Bind(R.id.tv_empty_memo)
    TextView tv_empty_memo;


    @Bind(R.id.lin_already_scan)
    LinearLayout lin_already_scan;

    @Bind(R.id.ns_view)
    NestedScrollView ns_view;

    @Bind(R.id.tv_pihao)
    TextView tv_pihao;

    @Bind(R.id.tv_yxq)
    TextView tv_yxq;

    @Bind(R.id.tv_jgh)
    TextView tv_jgh;

    @Bind(R.id.tv_orderId)
    TextView tv_orderId;

    @Bind(R.id.tv_orderTime)
    TextView tv_orderTime;

    @Bind(R.id.lin_add_drugs)
    LinearLayout lin_add_drugs;

    @Bind(R.id.tv_he)
    TextView tv_he;

    @Bind(R.id.add_Group)
    LinearLayout add_Group;

    List<CpreSendDrug> list;

    private DrugGiveConfirmContract.Present mPresent;
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_drug_give_confim;
    }

    @OnClick({R.id.img_back, R.id.lin_already_scan, R.id.tv_end_confim})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.lin_already_scan://
                Bundle b = new Bundle();
                b.putInt("type", CaptureActivity.TYPE_RE);
                b.putString("title", getString(R.string.ygypsm));
                showActivityForResult(CaptureActivity.class, b, Constants.RESULT_CAPTER);
                break;
            case R.id.tv_end_confim:
                showActivity(CarePlanActivity.class, null);
                break;
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null && bundle.containsKey("list")) {
            list = (List<CpreSendDrug>) bundle.getSerializable("list");
            setDrugData(list);
        }
    }

    @Override
    public void setData() {
        super.setData();
        tv_empty_memo.post(new Runnable() {
            @Override
            public void run() {
                int height = ns_view.getBottom() - add_Group.getTop();
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) tv_empty_memo.getLayoutParams();
                layoutParams.height = height;
                tv_empty_memo.setLayoutParams(layoutParams);
            }
        });
    }


    private void setDrugData(List<CpreSendDrug> list) {
        int all = 0;
        for (CpreSendDrug cpreSendDrug : list) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.drug_span_item, null);
            TextView tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            TextView tv_he = (TextView) itemView.findViewById(R.id.tv_he);
            tv_name.setText(cpreSendDrug.getName());
            tv_he.setText(cpreSendDrug.getQuantity() + "");
            all += cpreSendDrug.getQuantity();
            lin_add_drugs.addView(itemView);
        }
        tv_he.setText(all + "");
    }

    @Override
    public void setPresenter(DrugGiveConfirmContract.Present presenter) {
        mPresent = new DrugGiveConfirmPresenter(this,this);
    }

    @Override
    public void showData(DrugCode2Info info) {

    }
}
